package com.pankajm.gre;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pankajm.common.Files;

public class WordListLoader {
	
	private static final int MAX_NUM_WORDLISTS = 50;
	private final Map<Integer, List<WordItem>> wordlistMap;
	public final String wordlistsDirectory;
	
	public WordListLoader(String wordlistsDirectory) {
		 this.wordlistsDirectory = wordlistsDirectory;
		wordlistMap = new HashMap<Integer, List<WordItem>>();
	}
	
	public void loadWordLists() throws IOException {
		for (int i = 1; i <= MAX_NUM_WORDLISTS; i++) {
			loadWordList(i);
		}
	}
	
	public void loadWordList(int wordListNumber) throws IOException {
		loadWordList(this.wordlistsDirectory, wordListNumber);
	}
	
	public void loadWordList(String dirPath, int wordListNumber) throws IOException {
		List<String> lines = Files.readLines(dirPath, "WordList" + wordListNumber + ".txt");
		List<WordItem> wordItems = new ArrayList<WordItem>();
		
		for (String line : lines) {
			WordItem wordItem = WordItem.fromString(line);
			wordItems.add(wordItem);
		}
		
		wordlistMap.put(wordListNumber, wordItems);
	}
	
	public List<WordItem> getWordList(int wordListNumber) {
		return wordlistMap.get(wordListNumber);
	}
}
